﻿using System;

namespace Wulong.Phone.Dianping.Utils
{
    
    public class YqlHelper
    {
        private const string YqlBaseUrl = "http://query.yahooapis.com/v1/public/yql?q=";
        //private const string YqlAlltablesEnv = "&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
        private const string YqlSelect = "select * from {0}({1},{2})";

        public static Uri HtmlQuery(string url, string xpath, int start=0, int offset=10)
        {      
            return
                new Uri(
                    YqlBaseUrl+ 
                    ConvertToYqlUri(
                    string.Format(YqlSelect, "html", start, offset) + 
                    " where url=\"" + 
                    url+ "\" and xpath=\""+ xpath+ "\""));
        }

        private static string ConvertToYqlUri(string query)
        {
            return query
                .Replace("=", "%3D")
                .Replace(":", "%3A")
                .Replace("/", "%2F")
                .Replace("?", "%3F")
                .Replace("&", "%26")
                .Replace("\"", "%22");
        }
    }
}
